<template>
  <div class="newProject">
    <div style="width: 95%; margin: auto;margin-top: 20px;">
      <div style="width: 100%;height: 46px;align-items: center;border-bottom: 2px solid #DCDFE6;margin-bottom: 30px; display: flex;
  align-items: center;  ">
        <span
          style="font-family: PingFangSC-Medium;font-weight: 500;font-size: 16px;color: #303133;line-height: 24px;">编辑供应商</span>
      </div>
      <div>
        <el-form ref="form" :model="supplierInfo" :rules="rules">
          <el-row type="flex" justify="space-between"
            style="border: 1px solid #DCDFE6;height: 56px;background-color: #F5F7FA;">
            <el-col :span="5" class="colLeft">
              <span class="required">供应商名称：</span>
            </el-col>
            <el-col :span="6" class="colRight">
              <el-form-item label="" prop="name">
                <el-input v-model="supplierInfo.name" placeholder="请输入供应商名称"
                  style="height: 28px !important;"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="5" class="colLeft">
              <span class="required">法定代表人：</span>
            </el-col>
            <el-col :span="6" class="colRight">
              <el-form-item label="" prop="artificialPerson">
                <el-input v-model="supplierInfo.artificialPerson" placeholder="请输入法定代表人"
                  style="height: 28px !important;"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row type="flex" justify="space-between"
            style="border: 1px solid #DCDFE6;height: 56px;background-color: #F5F7FA;">
            <el-col :span="5" class="colLeft">
              <span class="required">资质：</span>
            </el-col>
            <el-col :span="6" class="colRight">
              <el-form-item label="" prop="credential">
                <el-input v-model="supplierInfo.credential" placeholder="请输入资质"
                  style="height: 28px !important;"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="5" class="colLeft">
              <span class="required">组织机构代码：</span>
            </el-col>
            <el-col :span="6" class="colRight">
              <el-form-item label="" prop="code">
                <el-input v-model="supplierInfo.code" placeholder="请输入组织机构代码"
                  style="height: 28px !important;"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row type="flex" justify="space-between"
            style="border: 1px solid #DCDFE6;height: 56px;background-color: #F5F7FA;">
            <el-col :span="5" class="colLeft">
              <span class="required">开户行：</span>
            </el-col>
            <el-col :span="6" class="colRight">
              <el-form-item label="" prop="bank">
                <el-input v-model="supplierInfo.bank" placeholder="请输入开户行" style="height: 28px !important;"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="5" class="colLeft">
              <span class="required">账号：</span>
            </el-col>
            <el-col :span="6" class="colRight">
              <el-form-item label="" prop="account">
                <el-input v-model="supplierInfo.account" placeholder="请输入账号"
                  style="height: 28px !important;"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row type="flex" justify="space-between"
            style="border: 1px solid #DCDFE6;height: 56px;background-color: #F5F7FA;">
            <el-col :span="5" class="colLeft">
              <span class="required">联系人：</span>
            </el-col>
            <el-col :span="6" class="colRight">
              <el-form-item label="" prop="contactor">
                <el-input v-model="supplierInfo.contactor" placeholder="请输入联系人"
                  style="height: 28px !important;"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="5" class="colLeft">
              <span class="required">联系电话：</span>
            </el-col>
            <el-col :span="6" class="colRight">
              <el-form-item label="" prop="telephone">
                <el-input v-model="supplierInfo.telephone" oninput="if(value.length>11)value=value.slice(0,11)"
                  placeholder="请输入联系电话" style="height: 28px !important;"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row type="flex" justify="space-between"
            style="border: 1px solid #DCDFE6;height: 56px;background-color: #F5F7FA;">
            <el-col :span="5" class="colLeft">
              <span class="">电子邮件：</span>
            </el-col>
            <el-col :span="6" class="colRight">
              <el-form-item label="" prop="email">
                <el-input v-model="supplierInfo.email" placeholder="请输入电子邮件"
                  style="height: 28px !important;"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="5" class="colLeft">
              <span class="">传真：</span>
            </el-col>
            <el-col :span="6" class="colRight">
              <el-form-item label="" prop="fax">
                <el-input v-model="supplierInfo.fax" placeholder="请输入传真" style="height: 28px !important;"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row type="flex" justify="space-between"
            style="border: 1px solid #DCDFE6;height: 56px;background-color: #F5F7FA;">
            <el-col :span="5" class="colLeft">
              <span class="required">经营范围：</span>
            </el-col>
            <el-col :span="6" class="colRight">
              <el-form-item label="" prop="businessScope">
                <el-input v-model="supplierInfo.businessScope" placeholder="请输入经营范围"
                  style="height: 28px !important;"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="5" class="colLeft">
              <span class="required">注册地址：</span>
            </el-col>
            <el-col :span="6" class="colRight">
              <el-form-item label="" prop="address">
                <el-input v-model="supplierInfo.address" placeholder="请输入注册地址"
                  style="height: 28px !important;"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row type="flex" justify="space-between"
            style="border: 1px solid #DCDFE6;height: 100px;background-color: #F5F7FA;">
            <el-col :span="5" class="colLeft">
              <span class="required">主要成员及职务：</span>
            </el-col>
            <el-col :span="18" class="colRight">
              <el-form-item label="" prop="members">
                <el-input type="textarea" :rows="3" placeholder="请输入主要成员及职务" v-model="supplierInfo.members">
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row type="flex" justify="space-between"
            style="border: 1px solid #DCDFE6;height: 100px;background-color: #F5F7FA;">
            <el-col :span="5" class="colLeft">
              <span class="required">主要业绩：</span>
            </el-col>
            <el-col :span="18" class="colRight">
              <el-form-item label="" prop="achievements">
                <el-input type="textarea" :rows="3" placeholder="请输入主要业绩" v-model="supplierInfo.achievements">
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row type="flex" justify="space-between"
            style="border: 1px solid #DCDFE6;height: 86px;background-color: #F5F7FA;">
            <el-col :span="5" class="colLeft">
              <span class="required">附件：</span>
            </el-col>
            <el-col :span="18" class="colRight">
              <el-upload :auto-upload="false" action="javascript:void(0)" :on-remove="handleRemove"
                :file-list="supplierInfo.annexs" :on-change="handleChange" :on-preview="downLoad">
                <el-button size="small" type="primary">点击上传</el-button>
              </el-upload>
            </el-col>
          </el-row>
          <el-row type="flex" justify="space-between"
            style="border: 1px solid #DCDFE6;height: 140px;background-color: #F5F7FA;">
            <el-col :span="5" class="colLeft">
              <span>备注：</span>
            </el-col>
            <el-col :span="18" class="colRight">
              <el-input type="textarea" :rows="5" placeholder="请输入备注" v-model="supplierInfo.remark">
              </el-input>
            </el-col>
          </el-row>
          <el-form-item style="margin-top: 30px;">
            <el-button type="primary" @click="onSubmit" style="background-color: #47ACF4;">确定</el-button>
            <el-button type="primary" @click="cancel"
              style="margin-left: 30px;background: transparent; color: #47ACF4;margin-right:24px ;">取消</el-button>
            <!-- <el-button>取消</el-button> -->
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { editSupplier } from "@/api/outsourcingManagement/supplier";
export default {
  components: {
  },
  props: {
    supplierInfo: {
      type: Object,
      required: false,
    }
  },
  data() {
    var checkPhone = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('手机号不能为空'));
      } else {
        const reg = /^1[3|4|5|7|8][0-9]\d{8}$/
        console.log(reg.test(value));
        if (reg.test(value)) {
          callback();
        } else {
          return callback(new Error('请输入正确的手机号'));
        }
      }
    };
    return {
      fileList: [],
      rules: {
        name: [
          { required: true, message: '请输入供应商名称', trigger: 'blur' },
        ],
        bank: [
          { required: true, message: '请输入开户行', trigger: 'blur' },
        ],
        credential: [
          { required: true, message: '输入资质', trigger: 'blur' },
        ],
        account: [
          { required: true, message: '请输入账号', trigger: ["blur", "change"] },
          { type: 'number', message: '请输入数字类型', trigger: 'blur' },
        ],
        contactor: [
          { required: true, message: '请输入联系人', trigger: 'blur' },
        ],
        telephone: [
          { validator: checkPhone, trigger: 'blur' }
        ],
        email: [{ type: "email", message: "请输入正确的邮箱地址", trigger: ["blur", "change"] }
        ],
        businessScope: [
          { required: true, message: '请选择经营范围', trigger: 'blur' },
        ],
        address: [
          { required: true, message: '请输入注册地址', trigger: 'blur' },
        ],
        members: [
          { required: true, message: '请输入成员及职务', trigger: 'blur' },
        ],
        achievements: [
          { required: true, message: '请输入主要业绩', trigger: 'blur' },
        ],
      },
    }
  },
  mounted() {
    this.fileList = this.supplierInfo.annexs;
  },
  methods: {
    async onSubmit() {
      console.log('filelist', this.fileList)
      if (this.fileList.length > 0 && this.fileList[0]?.status !== 'success') {
        let result = await this.fileUpload(this.fileList);
        this.fileList = [{
          size: result.size,
          status: result.status,
          name: result.originalFilenames[0],
          path: result.fileNames[0],
          id: result.attachmentIds[0]
        }]
      }
      this.supplierInfo.annexIdValues = this.fileList.map(item => item.id);
      editSupplier(this.supplierInfo).then(res => {
        if (res.code = 200) {
          alert("编辑项目成功！")
          this.closeDialog()
        }
      });
    },
    changeSel() {
      this.$forceUpdate()
    },
    closeDialog() {
      this.fileList = []
      this.$emit('close-dialog'); // 触发自定义事件
    },
    //附件移除
    handleRemove(file, fileList) {
      this.fileList = fileList;
    },
    // 材料改变
    handleChange(file, fileList) {
      const res = this.beforeAvatarUpload(file, fileList);
      if (!res || fileList.length >= 2) {
        fileList.shift();
      }
      this.fileList = fileList;
    },
    // 上传前控制
    beforeAvatarUpload(file, fileList) {
      const isLt2M = file.size / 1024 / 1024 < 100;
      if (!isLt2M) {
        this.$message.error(`上传图片大小不能超过100MB!`);
      }
      return isLt2M;
    },
    // 下载附件
    downLoad(file) {
      return downloadGet(this.downBaseUrl + file.path, {}, file.name);
    },
  }
}
</script>

<style scoped>
.newProject {
  width: 95%;
  height: 100%;
  background-color: #FFFFFF;
  margin-top: 20px;

  .colRight {

    padding: 10px;
    background-color: #FFFFFF;
  }

  .colLeft {
    /* border: 1px solid #DCDFE6; */
    background-color: #F5F7FA;
    text-align: center;
    margin: auto;
  }
}

span.required::after {
  content: "*";
  color: #f56c6c;
  margin-right: 4px;
}
</style>

<style>
.el-form-item__error {
  z-index: 999 !important;
}
</style>
